var common = (function () {


    var bashUrl = 'http://ztaudio-activity.kekestar.cn';//正式服api
    // var bashUrl = 'http://test-kkaudio-api.ikeke.ltd:5083';//测试服api
    // var bashUrl = 'http://ztaudioactivity.com';//本地api
    var API = {
        index: "lucky/index",
    }

    var getUrlParam = function getUrlParam(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);  //匹配目标参数
        if (r != null) return unescape(r[2]);
        return null; //返回参数值
    }

    /*
     * 判断object是否空，未定义或null
     * @param object
     * @return
     */
    var checkEmpty = function objectIsNull(obj) {
        if (obj == "" || typeof (obj) == "undefined" || obj == null) {
            return true;
        } else {
            return false;
        }
    }
    var getRequest = function (data, url, callBack) {
        $.ajax({
            url: bashUrl + '/' + url,
            data: data,
            contentType: 'application/json;charset=UTF-8',
            dataType: "json",
            type: "get",
            success: function (data) {
                callBack(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    };


    var postRequest = function (data, url, callBack) {
        $.ajax({
            url: bashUrl + '/' + url,
            data: JSON.stringify(data),
            contentType: 'application/json;charset=UTF-8',
            dataType: "json",
            type: "post",
            success: function (data) {
                callBack(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    };


    //适配
    function beforeMount() {
        var doc = document;
        var win = window;
        var docEl = doc.documentElement,
            resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
            recalc = function () {
                var clientWidth = docEl.clientWidth;
                if (!clientWidth) return;
                docEl.style.fontSize = 16 * (clientWidth / 375) + 'px';
            };
        if (!doc.addEventListener) return;
        win.addEventListener(resizeEvt, recalc, false);
        doc.addEventListener('DOMContentLoaded', recalc, false);
    }


    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURI(r[2]);
        return '';
    }

    /**
     * 获取当前时间
     *          format=1 精确到天
     *          format=2 精确到分
     * */
    function getCurrentDate(format) {
        let now = new Date();

        let year = now.getFullYear(); //得到年份
        let month = now.getMonth();//得到月份
        let date = now.getDate();//得到日期
        let day = now.getDay();//得到周几
        let hour = now.getHours();//得到小时
        let minu = now.getMinutes();//得到分钟
        let sec = now.getSeconds();//得到秒

        month = month + 1;
        if (month < 10) month = "0" + month;
        if (date < 10) date = "0" + date;
        if (hour < 10) hour = "0" + hour;
        if (minu < 10) minu = "0" + minu;
        if (sec < 10) sec = "0" + sec;

        let time = "";

        //精确到天
        if (format === 1) {
            time = year + "-" + month + "-" + date;
        }
        //精确到分
        else if (format === 2) {
            time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
        }

        return time;
    } /*
     * 判断object是否空，未定义或null
     * @param object
     * @return
     */
    var isEmpty = function (obj) {
        //检验null和undefined
        if (!obj && obj !== 0 && obj !== '') {
            return true;
        }
        //检验数组
        if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {
            return true;
        }
        //检验对象
        if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {
            return true;
        }
        return false;
    }


    return {
        API: API,
        bashUrl: bashUrl,
        postRequest: postRequest,
        getRequest: getRequest,
        getUrlParam: getUrlParam,
        isEmpty: isEmpty,
    }
}());
